﻿using Masuit.Tools;
using MySqlConnector;
using System.Diagnostics;

namespace Tgather
{
    public partial class AddProjectForm : Form
    {
        SqlSugarHelper mySQL = new SqlSugarHelper();
        public AddProjectForm()
        {
            InitializeComponent();
        }

        private void confirmButton_Click(object sender, EventArgs e)
        {
            if (startTime.Text.IsNullOrEmpty())
            {
                error("请选择开始时间");
                return;
            }

            if (endTime.Text.IsNullOrEmpty())
            {
                error("请选择结束时间");
                return;
            }

            if (unit.Text.IsNullOrEmpty())
            {
                error("请输入单位");
                return;
            }

            if (constructionType.Text.IsNullOrEmpty())
            {
                error("请选择施工类型");
                return;
            }

            if (pName.Text.IsNullOrEmpty())
            {
                error("请输入任务名称");
                return;
            }

            if (pCharge.Text.IsNullOrEmpty())
            {
                error("请输入负责人");
                return;
            }
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["p_name"] = pName.Text;
            int count = mySQL.count(where);
            if (count > 0)
            {
                Alert.Error("任务已存在");
                return;
            }
            // 获取当前时间
            DateTime now = DateTime.Now;

            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            string p_id = Convert.ToInt64(ts.TotalSeconds).ToString();
            Project project = new Project();
            project.p_id = p_id;
            project.start_time = Convert.ToDateTime(startTime.Text);
            project.end_time = Convert.ToDateTime(endTime.Text);
            project.unit = unit.Text;
            project.construction_type = constructionType.Text;
            project.p_name = pName.Text;
            project.p_charge = pCharge.Text;
            if (memo.Text.Length > 0)
            {
                project.memo = memo.Text;
            }
            project.create_time = now;
            project.report_status = 0;
            int res = mySQL.insert(project);
            if (res > 0)
            {
               IndexForm frm = (IndexForm)this.Owner!;
               frm.p_name = pName.Text;
               frm.freshProjectInfo();
               this.Close(); 
            }

            ProjectID pId = new ProjectID();
            pId.p_id = p_id;
            res = mySQL.insert(pId);
        }

        /// <summary>Error消息
        /// 
        /// </summary>
        /// <param name="msg"></param>
        private void error(string msg)
        {
            Alert.Error(msg);
        }

        private void AddProjectForm_Load(object sender, EventArgs e)
        {

        }
    }
}
